package com.jketing.poi.word;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jketing.entity.ams.work.MaizeExperiment;
import com.jketing.entity.ams.work.Repetition;
import com.jketing.entity.ams.work.Variety;
import com.jketing.service.ams.work.MaizeExperimentService;

public class MaizeExperimentWordBuilder {

	private WordTemplate template;
	
	private int getRank(List<Variety> list,String name) {
		int result = -1;
		List<Variety> sort = new ArrayList<Variety>();
		sort.addAll(list);
		Collections.sort(sort,new Comparator<Variety>() {
			@Override
			public int compare(Variety v1, Variety v2) {
				return (int)(v2.getYieldPerMu()-v1.getYieldPerMu());
			}
        });
		for(int i=0;i<sort.size();i++) {
			if(name.equals(sort.get(i).getName())) {
				result = i;
				break;
			}
		}
		return result;
	}

	public MaizeExperimentWordBuilder(InputStream inputStream) throws IOException {
		template = new WordTemplate(inputStream);
	}
	
	public InputStream wordBuilder(MaizeExperimentService service,String id,String path) throws IOException{
		InputStream in = null;
        Map<String, Object> wordDataMap = new HashMap<String, Object>();// 存储报表全部数据
        Map<String, Object> parametersMap = new HashMap<String, Object>();// 存储报表中不循环的数据
        MaizeExperiment test = service.getOne(id);
        
        parametersMap.put("name", test.getName());
        parametersMap.put("locale", test.getLocale());
        parametersMap.put("altitude", test.getAltitude());
        parametersMap.put("varietyCount", test.getVarietyNumber()-1);
        parametersMap.put("contrast", test.getContrast());
        parametersMap.put("describe", test.getDescribe());
        parametersMap.put("sowingTime", test.getSowingTime());
        parametersMap.put("firstFertilizationTtime", test.getFirstFertilizationTtime());
        parametersMap.put("secondFertilizationTtime", test.getSecondFertilizationTtime());
        parametersMap.put("harvestDate", test.getHarvestDate());
        String varietyNames = "";
        List<Variety> list1 = service.getVarietyList(id);
        List<Variety> list2 = new ArrayList<Variety>();
        Variety ck = null;
        for(int i=0;i<list1.size();i++){
        	Variety item = list1.get(i);
        	varietyNames += item.getName();
        	if(i<list1.size()-1) varietyNames += "；";
        	if(!test.getContrast().equals(item.getName())){
        		list2.add(item);//除对照之外的品种
        	}else{
        		ck = item;
        	}
        }
        parametersMap.put("varietyNames", varietyNames);
        Collections.sort(list2,new Comparator<Variety>() {
			@Override
			public int compare(Variety v1, Variety v2) {
				return (int)(v2.getYieldPerMu()-v1.getYieldPerMu());
			}
        });
        
        parametersMap.put("minYieldPerMu", list2.get(list2.size()-1).getYieldPerMu());
        parametersMap.put("maxYieldPerMu", list2.get(0).getYieldPerMu());
        parametersMap.put("CKYieldPerMu", ck.getYieldPerMu());
        
        parametersMap.put("varietyName1", list2.get(0).getName());
        parametersMap.put("YieldPerMu1", list2.get(0).getYieldPerMu());
        parametersMap.put("grainColor1", list2.get(0).getGrainColor());
        parametersMap.put("grainType1", list2.get(0).getGrainType());
        parametersMap.put("spikeType1", list2.get(0).getSpikeType());
        parametersMap.put("axialColour1", list2.get(0).getAxialColour());
        parametersMap.put("spikeLength1", list2.get(0).getSpikeLength());
        parametersMap.put("spikeWide1", list2.get(0).getSpikeType());
        parametersMap.put("spikeRowNumber1", list2.get(0).getSpikeRowNumber());
        parametersMap.put("rowGrainNumber1", list2.get(0).getRowGrainNumber());
        parametersMap.put("thousandGrainWeight1", list2.get(0).getThousandGrainWeight());
        float difference = list2.get(0).getYieldPerMu() - ck.getYieldPerMu();        
        String prefix = "增";
        if(difference < 0){
        	difference = Math.abs(difference);
        	prefix = "减";
        }
        float percentage = Math.round((difference/ck.getYieldPerMu()*100)*100) /100;
        parametersMap.put("difference1", prefix + difference);
        parametersMap.put("percentage1", prefix + percentage);
        
        parametersMap.put("varietyName2", list2.get(1).getName());
        parametersMap.put("YieldPerMu2", list2.get(1).getYieldPerMu());
        parametersMap.put("grainColor2", list2.get(1).getGrainColor());
        parametersMap.put("grainType2", list2.get(1).getGrainType());
        parametersMap.put("spikeType2", list2.get(1).getSpikeType());
        parametersMap.put("axialColour2", list2.get(1).getAxialColour());
        parametersMap.put("spikeLength2", list2.get(1).getSpikeLength());
        parametersMap.put("spikeWide2", list2.get(1).getSpikeType());
        parametersMap.put("spikeRowNumber2", list2.get(1).getSpikeRowNumber());
        parametersMap.put("rowGrainNumber2", list2.get(1).getRowGrainNumber());
        parametersMap.put("thousandGrainWeight2", list2.get(1).getThousandGrainWeight());
        difference = list2.get(1).getYieldPerMu() - ck.getYieldPerMu();        
        prefix = "增";
        if(difference < 0){
        	difference = Math.abs(difference);
        	prefix = "减";
        }
        percentage = Math.round((difference/ck.getYieldPerMu()*100)*100) /100;
        parametersMap.put("difference2", prefix + difference);
        parametersMap.put("percentage2", prefix + percentage);
        
        List<Map<String, Object>> table1 = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        for(Variety v:list1) {
        	map = new HashMap<>();
            map.put("name", v.getName());
            map.put("spikeLength", v.getSpikeLength());
            map.put("spikeWide", v.getSpikeWide());
            map.put("spikeRowNumber", v.getSpikeRowNumber());
            map.put("rowGrainNumber", v.getRowGrainNumber());
            map.put("alopeciaAcuity", v.getAlopeciaAcuity());
            map.put("spikeType", v.getSpikeType());
            map.put("seedYield", v.getSeedYield());
            map.put("thousandGrainWeight", v.getThousandGrainWeight());
            table1.add(map);
        }

        List<Map<String, Object>> table2 = new ArrayList<Map<String, Object>>();
        for(Variety v:list1) {
        	map = new HashMap<>();
            map.put("name", v.getName());
            map.put("spikeGrainRot", v.getSpikeRot()+v.getGrainRot());
            table2.add(map);
        }

        List<Map<String, Object>> table3 = new ArrayList<Map<String, Object>>();
        for(Variety v:list1) {
        	map = new HashMap<>();
            map.put("name", v.getName());
            int index = 1;
            float total = 0;
            for(Repetition r:v.getRepetitions()) {
            	map.put("plotYield"+index, r.getPlotYield());
            	total = total + r.getPlotYield();
            	index++;
            }
            map.put("plotYieldTotal", total);
            map.put("plotYieldAvg", v.getPlotYield());
            map.put("yieldPerMu", v.getYieldPerMu());
            difference = v.getYieldPerMu() - ck.getYieldPerMu();
            percentage = Math.round((difference/ck.getYieldPerMu()*100)*100) /100;
            map.put("difference", difference);
            map.put("percentage", percentage);
            map.put("rank", getRank(list1,v.getName()));
            table3.add(map);
        }

        wordDataMap.put("table1", table1);
        wordDataMap.put("table2", table2);
        wordDataMap.put("table3", table3);
        wordDataMap.put("parametersMap", parametersMap);

        // 替换数据
        template.replaceDocument(wordDataMap);

        //生成文件
        File outputFile=new File(path);//改成你本地文件所在目录
        FileOutputStream fos  = new FileOutputStream(outputFile);
        template.getDocument().write(fos);
        fos.close();
		return in;
	}
}
